package org.csu.mall.controller.front;

import org.apache.commons.lang3.StringUtils;
import org.csu.mall.common.CONSTANT;
import org.csu.mall.common.UniformResponse;
import org.csu.mall.entity.Account;
import org.csu.mall.entity.Cart;
import org.csu.mall.entity.Commodity;
import org.csu.mall.service.ICartService;
import org.csu.mall.vo.CartCommodityListVO;
import org.csu.mall.vo.CartCommodityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cart/")
public class CartController {

    @Autowired
    private ICartService cartService;

    @PostMapping("add_item")
    @ResponseBody
    public UniformResponse<String> addCart(
            String itemId,
            @RequestParam(name = "quantity", defaultValue = "1") Integer quantity,
            HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);

        //测试代码
//        Account account = new Account();
//        account.setUserId(1001);
        return cartService.addCart(itemId, quantity, account);
    }

    @GetMapping("remove_item")
    @ResponseBody
    public UniformResponse<String> removeCart(Integer id, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        //测试代码
//        Account account1 = new Account();
//        account1.setUserId(1001);
        return cartService.removeCart(id, account);
    }

    @PostMapping("add_quantity")
    @ResponseBody
    public UniformResponse<String> addQuantity(Integer id, Integer quantity, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        //测试代码
//        Account account1 = new Account();
//        account1.setUserId(1001);
        return cartService.addQuantity(id, quantity, account);
    }

    @GetMapping("cart_item")
    @ResponseBody
    public UniformResponse<CartCommodityListVO> lookCart(
            @RequestParam(name = "pageSize", defaultValue = "5") int pageSize,
            @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
            HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
//        测试代码
//        Account account1 = new Account();
//        account1.setUserId(1001);
        return cartService.lookCart(account, pageSize, pageNum);
    }

    // todo 待测
    @GetMapping("cart_purchase")
    @ResponseBody
    public UniformResponse<CartCommodityVO> cartPurchase(Integer id, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        return cartService.cartPurchase(id, account);
    }

    @PostMapping("update_checked")
    @ResponseBody
    public UniformResponse<String> updateChecked(Integer id, Integer checked, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        return cartService.updateChecked(id, checked, account);
    }

    @PostMapping("purchase_checked")
    @ResponseBody
    public UniformResponse<Map<String, String>> purchaseCheckedCommodity(HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        return cartService.purchaseCheckedCommodity(account);
    }

}